<?php
namespace Home\Controller;
use Think\Controller;
use Think\Crypt\Driver\Think;

/**
 * Created by PhpStorm.
 * User: jieai
 * Date: 2017-06-03
 * Time: 10:53
 */
class LeaveController extends MyController {
    public function Index(){
        $members = M('member')->field('id,name')->select();
        $this->assign('members', $members);
        $this->display();
    }

    public function add()
    {
        $members = M('member')->field('id,name')->select();
        $this->assign('members', $members);
        $this->display();
    }

    public function addpost()
    {
        $memberid = I('memberid');//会员id
        $sdate = I('fftime');//日期的值
        $timee = I('timee');//1对应8点到12点;2对应2点到6点,3对应8点到6点,4对应自定义
        $starttime = I('stime');//开始时间
        $donetime = I('dtime');//结束时间
        switch ($timee) {
            case '1':
                $starttime = '08:00';
                $donetime = '12:00';
                break;
            case '2':
                $starttime = '14:00';
                $donetime = '18:00';
                break;
            case '3':
                $starttime = '08:00';
                $donetime = '18:00';
                break;
            default:
                $starttime = I('stime');//开始时间
                $donetime = I('dtime');//结束时间

        }
        $leixing = I('leixing');//类型,0是请假,1是加班

        $fullstime = $sdate . " " . $starttime;
//        P($fullstime);
//        die;
        $fulldtime = $sdate . " " . $donetime;
        $gg = strtotime($donetime) - strtotime($starttime);
        $ggg = $gg / 60 / 60;//计算出时长,只能是当天
        ///如果是请假,超过8小时按8小时计算.如果是加班,原样记录

        $halfdur = round($ggg * 2);//按照半小时计算
        $duration = $halfdur;
        if ($leixing == 0) {
            $duration = $halfdur >= 16 ? 16 : $halfdur;
            ///如果是请假,超过8小时按8小时计算.如果是加班,原样记录
        }
        $level = M('level');
        $level->member_id = $memberid;
        $level->starttime = strtotime($fullstime);
        $level->stoptime = strtotime($fulldtime);
        $level->leixing = $leixing;
        $level->pend = 0;
        $level->status = 0;
        $level->createtime = time();
        $level->duration = $duration;
        $level->beizhu = I('beizhu');

        $result = $level->add();
        $data['info'] = "ok!";
        if ($result) {
            $this->ajr("fuck!");
        }
    }

    public function getlist()
    {
        $starttime = I('starttime');
        $donetime = I('donetime');
        $pendid = I('pendid', -1);
        $map = array();
        if ($starttime) {
            $starttime = strtotime($starttime);

            $map['starttime'][] = array('egt', $starttime);
        }
//        if ($donetime) {
//            $donetime = strtotime($donetime);
//            $map['starttime'][] = array('elt', $donetime);
//        }
        $map['pend'] = array('neq', $pendid);
        //endregion


        //region 可以根据传过来的memberid来查找gsrecord的id
        $memids = I('memid', array(0));
        $membermap = array();
        if (!in_array(0, $memids)) {

            $map['member_id'] = array('in', $memids);
        }
        //




        $page = I('page', 1);
        $rows = I('rows', 20);
        $offset = ($page - 1) * $rows;
        $map['status'] = 0;

        $leave = D('levelReation');
        $rowresult = $leave->relation(true)->where($map)->order('createtime desc  ')->limit($offset, $rows)->select();

        $total = M('level')->where($map)->count();
        $result = array();
        $result['total'] = $total;
        $result['rows'] = $rowresult;


        $this->ajaxReturn($result);

    }

    public function pendrecord()
    {
        //$this->Checklogin();
        if (IS_AJAX) {
            $rid = I('id');
            $record = M('level');
            $record->id = $rid;
            $record->pend = 1;
            $result = $record->save();
            if ($result) {
                $this->success('审核成功');
            }
            $this->error('审核失败!');

        }
    }

    public function deleterecord()
    {
        // $this->Checklogin();
        $id = I('id');
        if ($id) {
            $recordR = M('level');
            $recordR->id = $id;
            $recordR->status = 1;
            $result = $recordR->save();

            if ($result) {
                $this->success('删除成功');
            }
        }
        $this->error('什么也没做!');


    }

    public function getSumlist()
    {
        $this->Checklogin();
        //region 默认时间为最近一个月
        $stime = strtotime(I('stime', date('Y-M-d ', time() - 60 * 60 * 24 * 31)));
        $dtime = strtotime(I('dtime', date('Y-M-d', time())));


        $model = new \Think\Model();

        $sqlstr = "SELECT sss.member_id,gs_member.name,sss.aa AS jiaban,sss.bb AS qingjia from (SELECT b.member_id,aa,bb FROM ( SELECT member_id,sum(duration) as aa FROM gs_level WHERE leixing=1 AND pend=1 AND  gs_level.status=0 AND starttime BETWEEN $stime AND $dtime GROUP BY member_id) AS b
  LEFT JOIN
  (SELECT member_id,sum(duration) as bb FROM gs_level WHERE leixing=0 AND pend=1 AND  gs_level.status=0 AND starttime BETWEEN   $stime AND $dtime GROUP BY member_id) as c ON b.member_id=c.member_id
UNION
SELECT c.member_id,aa,bb FROM ( SELECT member_id,sum(duration) as aa FROM gs_level WHERE leixing=1 AND pend=1 AND  gs_level.status=0 AND starttime BETWEEN $stime AND $dtime GROUP BY member_id) AS b
RIGHT JOIN
 ( SELECT member_id,sum(duration) as bb FROM gs_level WHERE leixing=0 AND pend=1 AND  gs_level.status=0 AND starttime BETWEEN $stime AND $dtime GROUP BY member_id) as c ON b.member_id=c.member_id) AS sss,gs_member WHERE gs_member.id=sss.member_id;";
        //$result = $model->query($sqlstr);
        $result = $model->query("SELECT member_id ,gs_member.name, sum(if(leixing=1,duration,0))AS jiaban,sum(if(leixing=0,duration,0))AS qingjia  FROM gs_level,gs_member WHERE gs_level.member_id=gs_member.id AND pend=1 AND status=0 AND starttime BETWEEN $stime AND $dtime GROUP BY member_id");

        $this->ajaxReturn($result);

    }

}